package com.marsoft.test.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.marsoft.test.entity.CollateralMessageDetailEntity;
import com.marsoft.test.entity.WarehouseApplyEntity;
import com.marsoft.test.exception.DefinitionException;
import com.marsoft.test.service.WarehouseApplyService;
import com.marsoft.test.utils.ResultMap;
import com.marsoft.test.validation.ValidationGroups;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("WarehouseApplyController")
public class WarehouseApplyController {
    @Autowired
    private WarehouseApplyService warehouseApplyService;

    private  Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 入库申请查询
     * @param warehouseApplyStatus
     * @param warehouseApplyNo
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/findByApplyNoStatus")
    public ResultMap findByApplyNoStatus(@RequestParam Integer warehouseApplyStatus, @RequestParam String warehouseApplyNo,
                                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                                         @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        List<WarehouseApplyEntity> byApplyNoStatus = warehouseApplyService.findByApplyNoStatus(warehouseApplyStatus, warehouseApplyNo, page, pageSize);
        logger.info("输出"+byApplyNoStatus+"信");
        return ResultMap.ok().put("date",byApplyNoStatus);
    }

    /**
     * 押品保险信息详情
     * @param warehouseApplyNo
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/findCollateralMessageDetail")
    public ResultMap findCollateralMessageDetail(   @RequestParam String warehouseApplyNo,
                                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        List<CollateralMessageDetailEntity> collateralMessageDetail = warehouseApplyService.findCollateralMessageDetail(warehouseApplyNo,page,pageSize);
        logger.info("结果为:"+collateralMessageDetail);
        return ResultMap.ok().put("date",collateralMessageDetail);
    }
    /**
     * 新增入库申请
     * @param warehouseApplyEntity
     * @param result
     * @return
     */
    @RequestMapping(value = "/insertwarehouseApply")
    public ResultMap insertwarehouseApply(@Validated(ValidationGroups.insertwarehouseApply.class) WarehouseApplyEntity warehouseApplyEntity,
                                          BindingResult result){

        // 如果页面校验出错
        if(result.hasErrors()){

            // 错误信息
            List<FieldError> list = result.getFieldErrors();

            // 收集错误信息
            Map<String, String> res = list.stream().
                    collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage,(o1, o2)->o1));

            return ResultMap.error().put("msg",res);
        }

        if(1 == 0){
            throw new DefinitionException("业务异常");
        }

        warehouseApplyService.insertwarehouseApply(warehouseApplyEntity);
        return ResultMap.ok();
    }

    /**
     * 入库申请修改
     * @param warehouseApplyEntity
     * @param result
     * @return
     */
    @PostMapping(value = "/updatewarehouseApply")
    public ResultMap updatewarehouseApply(@Validated(ValidationGroups.updatewarehouseApply.class) WarehouseApplyEntity warehouseApplyEntity,
                                           BindingResult result){
        // 如果页面校验出错
        if(result.hasErrors()){

            // 错误信息
            List<FieldError> list = result.getFieldErrors();

            // 收集错误信息
            Map<String, String> res = list.stream().
                    collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage,(o1, o2)->o1));

            return ResultMap.error().put("msg",res);
        }

        if(1 == 0){
            throw new DefinitionException("业务异常");
        }
        warehouseApplyService.updatewarehouseApply(warehouseApplyEntity);
        return  ResultMap.ok();
    }

    /**
     * 根据入库申请编号删除
     * @param warehouseApplyNo
     * @return
     */
    @RequestMapping(value = "deletewarehouseApply")
    public ResultMap deletewarehouseApply(@RequestParam String warehouseApplyNo){
       warehouseApplyService.deletewarehouseApply(warehouseApplyNo);
        return ResultMap.ok();
    }
}
